package cookie

import (
	"gin-project-01/gin-step04/gin14-gorm-base/controllers"
	"gin-project-01/gin-step04/gin14-gorm-base/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type CookieController struct {
	controllers.BaseController //继承 controllers.BaseController
}

// CookieAdd
//
//	@Description: CookieAdd
//	@receiver u
//	@param c
func (u CookieController) CookieAdd(c *gin.Context) {
	aid := models.GenerateRandomString(32)
	bid := models.GenerateRandomString(32)
	c.SetCookie("aid", aid, 3600, "/", "localhost", false, true)
	c.SetCookie("bid", bid, 5, "/", "localhost", false, true)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"aid":     aid,
		"bid":     bid,
	})
}

// CookieGet
//
//	@Description: 获取cookie
//	@receiver u
//	@param c
func (u CookieController) CookieGet(c *gin.Context) {
	aid, _ := c.Cookie("aid")
	bid, _ := c.Cookie("bid")
	c.JSON(http.StatusOK, gin.H{
		"aid": aid,
		"bid": bid,
	})
}

// CookieDel
//
//	@Description: 删除cookie
//	@receiver u
//	@param c
func (u CookieController) CookieDel(c *gin.Context) {
	c.SetCookie("aid", "", -1, "/", "localhost", false, true)
	c.SetCookie("bid", "", -1, "/", "localhost", false, true)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}
